package com.ciei.dpagm.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ciei.dpagm.common.enums.VersionsRecordEnum;
import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.VersionsRecord;
import com.ciei.dpagm.mapper.VersionsRecordMapper;
import com.ciei.dpagm.util.APP;
import com.ciei.dpagm.util.DateUtil;
import com.ciei.dpagm.util.JsonUtil;
import com.ciei.dpagm.util.PageParam;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class VersionsRecordService extends BaseService<VersionsRecordMapper,VersionsRecord> {

    /**获取版本更新记录表格
     * @param page
     * @return
     */
    public JSONObject getVersionsRecordDataGrid(PageParam page) {
        Map<String, Object> params=page.getParams();
        params.put("isDelete", 0);
        params.put("versionType",VersionsRecordEnum.PC.getVersionType());
        Map<String,Object> sortFields= page.getSort();
        if(sortFields.isEmpty()){
            sortFields.put("createTime", APP.DESC);
        }

        int currentPage =page.getCurrentPage();
        int pageSize =page.getPageSize();
        IPage<VersionsRecord> versionsRecordIPage = findPageVue(currentPage, pageSize, params, new String[]{"versionsNumber"}, new String[]{"versionType"}, sortFields,null);
        List<VersionsRecord> versionsRecords = versionsRecordIPage.getRecords();

        JSONArray dataArray = new JSONArray();
        for (VersionsRecord versionsRecord:versionsRecords){
            HashMap<String, Object> versionsRecordMap = new HashMap<>();
            versionsRecordMap.put("versionsId",versionsRecord.getVersionsId());
            versionsRecordMap.put("createTime", DateUtil.dateToString(versionsRecord.getCreateTime(),DateUtil.FORMAT_LONG));
            versionsRecordMap.put("updateContent",versionsRecord.getUpdateContent());
            versionsRecordMap.put("versionsNumber",versionsRecord.getVersionsNumber());
            dataArray.add(versionsRecordMap);
        }
        JSONObject json = JsonUtil.getSuccess("OK");
        json.put(APP.DATA, dataArray);
        json.put(APP.TOTAL_COUNT, versionsRecordIPage.getTotal());
        json.put(APP.POS, (currentPage - 1) * pageSize);
        json.put(APP.CURRENT_PAGE, versionsRecordIPage.getCurrent());
        return json;
    }



    /**
     * 获取最新版
     * @param versionType
     * @return
     */
    public JSONObject getTheLatestVersion(Integer versionType){
        LambdaQueryWrapper<VersionsRecord> versionsRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        versionsRecordLambdaQueryWrapper.eq(VersionsRecord::getVersionType,versionType)
                .orderByDesc(VersionsRecord::getVersionsNumber);

        List<VersionsRecord> versionsRecords = list(versionsRecordLambdaQueryWrapper);


        String pattern = "^[0-9]+[0-9\\.]+[0-9]+$";

        List<VersionsRecord> patternVersionsRecords = versionsRecords.stream()
                .filter(versionsRecord -> versionsRecord.getVersionsNumber().matches(pattern))
                .collect(Collectors.toList());

        if (patternVersionsRecords.isEmpty()){
            return JsonUtil.getSuccess("").fluentPut(APP.DATA,"");
        }
        VersionsRecord versionsRecord = patternVersionsRecords.get(0);
        JSONObject versionsRecordJson = new JSONObject();
        versionsRecordJson.fluentPut("versionsId",versionsRecord.getVersionsId())
                .fluentPut("versionsNumber",versionsRecord.getVersionsNumber())
                .fluentPut("createTime",versionsRecord.getCreateTime())
                .fluentPut("updateContent",versionsRecord.getUpdateContent()==null?"":versionsRecord.getUpdateContent())
                .fluentPut("path",versionsRecord.getPath()==null?"":versionsRecord.getPath());
        return JsonUtil.getSuccess("").fluentPut(APP.DATA,versionsRecordJson);
    }

    /**
     * 保存版本记录数据
     * @param data
     * @return
     */
    public JSONObject saveVersionsRecordData(JSONObject data) {
        VersionsRecord versionsRecord = data.toJavaObject(VersionsRecord.class);
        save(versionsRecord);
        return JsonUtil.getSuccess("");
    }
}
